package com.coocaa.typhoon.api.v1.milvus.service.sync;

import cn.hutool.core.bean.BeanUtil;
import com.coocaa.cirrus.common.base.model.MetaDataModel;
import com.coocaa.cirrus.common.base.util.DateUtil;
import com.coocaa.cirrus.common.base.util.JsonUtil;
import com.coocaa.cirrus.common.third.filestore.config.TosProperties;
import com.coocaa.typhoon.api.third.volcengine.constant.EmbeddingTypeConstant;
import com.coocaa.typhoon.api.third.volcengine.model.EmbeddingQueryDTO;
import com.coocaa.typhoon.api.third.volcengine.provider.VolcEmbeddingProvider;
import com.coocaa.typhoon.api.v1.milvus.constant.MilvusConstant;
import com.coocaa.typhoon.api.v1.milvus.model.UserFileMilvusDTO;
import com.coocaa.typhoon.api.v1.milvus.service.UserFileMilvusUpdateService;
import com.coocaa.typhoon.api.v1.mysql.model.entity.UserFileEntity;
import com.coocaa.typhoon.api.v1.userfile.util.TosFinalUrlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author bijiahao
 * @date : 2025/9/25 11:32
 * @description
 */
@Service
public class UserFileSyncService {
    @Autowired
    private UserFileMilvusUpdateService userFileMilvusUpdateService;
    @Autowired
    private TosProperties tosProperties;
    @Autowired
    private VolcEmbeddingProvider volcEmbeddingProvider;

    public List<Float> questionVector(UserFileEntity entity) {
        String tosLink = TosFinalUrlUtil.getTosLink(tosProperties.getTosBucketName(), entity.getFilePath());
        EmbeddingQueryDTO embeddingQueryDTO = new EmbeddingQueryDTO();
        embeddingQueryDTO.setContent(tosLink);
        embeddingQueryDTO.setType(EmbeddingTypeConstant.IMAGE);
        return volcEmbeddingProvider.questionVector(embeddingQueryDTO);
    }


    public SortedMap<Long, Float> questionSparseVector(UserFileEntity entity) {
//        String question = entity.getBaseQuestion();
//        Set<String> questionSet = new HashSet<>();
//        questionSet.add(question);
//        Map<String, SortedMap<Long, Float>> questionVectorMap = bgeVectorService.getSparseVector(questionSet);
//        SortedMap<Long, Float> floats = questionVectorMap.get(question);
//        return floats;
        return null;
    }


    public void sync(UserFileEntity entity) {
        UserFileMilvusDTO res = buildUserFileMilvusDTO(entity);
        userFileMilvusUpdateService.update(MilvusConstant.USER_FILE, res);
    }


    public UserFileMilvusDTO queryById(String id) {
        UserFileMilvusDTO res = userFileMilvusUpdateService.queryById(MilvusConstant.USER_FILE, Long.parseLong(id), UserFileMilvusDTO.class);
        return res;
    }


    private UserFileMilvusDTO buildUserFileMilvusDTO(UserFileEntity entity) {
        UserFileMilvusDTO db = queryById(entity.getId());
        if (null == db) {
            db = new UserFileMilvusDTO();
            //设置公共参数
            handleSetCommon(db, entity);
            db.setId(Long.parseLong(entity.getId()));
            db.setTosVector(questionVector(entity));
//            db.setRemarkSparseVector(questionSparseVector(entity));
        } else {
            boolean isTosChange = isTosChange(db, entity);
            boolean isRemarkChange = isRemarkChange(db, entity);
            //设置公共参数
            handleSetCommon(db, entity);
            if (isTosChange) {
                db.setTosVector(questionVector(entity));
            }
            if (isRemarkChange) {
//                db.setRemarkSparseVector(questionSparseVector(entity));
            }
        }
        db.setStatus(1);
        return db;
    }


    private static void handleSetCommon(UserFileMilvusDTO db, UserFileEntity entity) {
        BeanUtil.copyProperties(entity, db);
        setMetaData(db, entity);
//        if (StringUtils.isBlank(db.getRemark())) {
//            db.setRemark("");
//        }
        db.setUploadTime(null == entity.getCreatedDate() ? System.currentTimeMillis() : entity.getCreatedDate().getTime());
    }

    private static void setMetaData(UserFileMilvusDTO dto, UserFileEntity entity) {
        if (StringUtils.isBlank(entity.getMetaDataJson())) {
            dto.setShootTime(0L);
            dto.setLocation("");
        }
        MetaDataModel metaDataModel = (MetaDataModel) JsonUtil.json2Object(entity.getMetaDataJson(), MetaDataModel.class);
        if (null == metaDataModel) {
            dto.setShootTime(0L);
            dto.setLocation("");
        } else {
            dto.setShootTime(StringUtils.isBlank(metaDataModel.getShootTime()) ? 0L : DateUtil.toTimestamp(metaDataModel.getShootTime()));
            dto.setLocation(StringUtils.isBlank(metaDataModel.getLocation()) ? "" : metaDataModel.getLocation());
        }
    }

    private static boolean isTosChange(UserFileMilvusDTO db, UserFileEntity entity) {
        //TODO:
        return !Objects.equals(db.getFilePath(), entity.getFilePath());
    }

    private static boolean isRemarkChange(UserFileMilvusDTO db, UserFileEntity entity) {
        //TODO:
//        return !Objects.equals(db.getRemark(), entity.getRemark());
        return false;
    }

}
